#!/bin/bash

# Flutter iOS Release 构建配置脚本
# 作用：解决 Xcode 打包时的 debug/release 配置冲突问题

echo "🍎 开始配置 Flutter iOS Release 构建..."
echo "======================================"

# 获取脚本所在目录（项目根目录）
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "📁 当前目录: $(pwd)"
echo "⏰ 开始时间: $(date)"

# 检查是否为 Flutter 项目
if [ ! -f "pubspec.yaml" ]; then
    echo "❌ 错误：当前目录不是 Flutter 项目！"
    read -p "按任意键退出..." -n1 -s
    exit 1
fi

# 检查 iOS 目录
if [ ! -d "ios" ]; then
    echo "❌ 错误：未找到 ios 目录！"
    read -p "按任意键退出..." -n1 -s
    exit 1
fi

echo "✅ 检测到 Flutter 项目和 iOS 支持"

# 显示项目信息
if command -v grep >/dev/null 2>&1; then
    PROJECT_NAME=$(grep "^name:" pubspec.yaml | cut -d' ' -f2)
    PROJECT_VERSION=$(grep "^version:" pubspec.yaml | cut -d' ' -f2)
    echo "📄 项目名称: $PROJECT_NAME"
    echo "🏷️  项目版本: $PROJECT_VERSION"
fi

echo ""

# 显示问题说明
echo "🐛 问题分析："
echo "   Flutter 项目当前配置为 debug 模式"
echo "   Xcode 打包需要 release 模式配置"
echo "   需要同步 Flutter 和 Xcode 的构建设置"
echo ""

echo "🔍 将执行以下操作："
echo "   1. 🧹 清理项目构建缓存"
echo "   2. 📦 重新获取依赖"
echo "   3. 🔧 配置 iOS Release 模式"
echo "   4. 🍎 同步 Xcode 项目设置"
echo "   5. 🎯 验证构建配置"
echo ""

echo "🚀 开始自动配置..."
echo ""

# 步骤 1: 清理项目
echo "🧹 步骤 1/5: 清理项目缓存..."
echo "----------------------------------------"
flutter clean
if [ $? -eq 0 ]; then
    echo "✅ 项目清理完成"
else
    echo "❌ 项目清理失败"
    read -p "按任意键退出..." -n1 -s
    exit 1
fi

echo ""

# 步骤 2: 获取依赖
echo "📦 步骤 2/5: 重新获取依赖..."
echo "----------------------------------------"
flutter pub get
if [ $? -eq 0 ]; then
    echo "✅ 依赖获取完成"
else
    echo "❌ 依赖获取失败"
    read -p "按任意键退出..." -n1 -s
    exit 1
fi

echo ""

# 步骤 3: 配置 iOS Release 模式
echo "🔧 步骤 3/5: 配置 iOS Release 模式..."
echo "----------------------------------------"
echo "正在执行: flutter build ios --config-only --release"
flutter build ios --config-only --release

if [ $? -eq 0 ]; then
    echo "✅ iOS Release 配置完成"
else
    echo "❌ iOS Release 配置失败，尝试备用方案..."
    # 备用方案：先执行一次完整构建
    flutter build ios --release --no-codesign
    if [ $? -eq 0 ]; then
        echo "✅ 备用方案执行成功"
    else
        echo "❌ 备用方案也失败了"
        read -p "按任意键退出..." -n1 -s
        exit 1
    fi
fi

echo ""

# 步骤 4: 同步 Xcode 项目设置
echo "🍎 步骤 4/5: 同步 Xcode 项目设置..."
echo "----------------------------------------"

# 检查并更新 CocoaPods
cd ios
echo "正在更新 CocoaPods 依赖..."
pod install --repo-update

if [ $? -eq 0 ]; then
    echo "✅ CocoaPods 依赖更新完成"
else
    echo "⚠️  CocoaPods 更新有问题，尝试修复..."
    pod deintegrate
    pod install
    if [ $? -eq 0 ]; then
        echo "✅ CocoaPods 修复成功"
    else
        echo "❌ CocoaPods 修复失败"
    fi
fi

cd ..
echo ""

# 步骤 5: 验证构建配置
echo "🎯 步骤 5/5: 验证构建配置..."
echo "----------------------------------------"

# 检查关键配置文件
echo "检查构建配置文件..."

if [ -f "ios/Flutter/Generated.xcconfig" ]; then
    echo "✅ Generated.xcconfig 存在"
else
    echo "❌ Generated.xcconfig 缺失"
fi

if [ -f "ios/Flutter/Release.xcconfig" ]; then
    echo "✅ Release.xcconfig 存在"
else
    echo "❌ Release.xcconfig 缺失"
fi

if [ -f "ios/Flutter/Debug.xcconfig" ]; then
    echo "✅ Debug.xcconfig 存在"
else
    echo "❌ Debug.xcconfig 缺失"
fi

# 检查 Pods 配置
if [ -f "ios/Pods/Pods.xcodeproj/project.pbxproj" ]; then
    echo "✅ Pods 项目配置正常"
else
    echo "❌ Pods 项目配置异常"
fi

echo ""
echo "🎉 Flutter iOS Release 配置完成！"
echo "======================================"

echo "📝 配置总结："
echo "   ✅ 清理了项目构建缓存"
echo "   ✅ 重新获取了 Flutter 依赖"
echo "   ✅ 配置了 iOS Release 模式"
echo "   ✅ 同步了 Xcode 项目设置"
echo "   ✅ 更新了 CocoaPods 依赖"

echo ""
echo "🍎 现在可以在 Xcode 中进行以下操作："
echo "   1. 打开 ios/玩真的.xcworkspace （或使用下面的自动打开选项）"
echo "   2. 选择 Runner > Generic iOS Device"
echo "   3. 选择 Product > Archive 开始打包"
echo "   4. 选择正确的构建配置（Release）"

echo ""
echo "💡 重要提示："
echo "   - 必须打开 .xcworkspace 文件，不是 .xcodeproj"
echo "   - 确保选择了真实设备或 Generic iOS Device"
echo "   - 构建配置必须选择 Release 模式"
echo "   - 如果还有问题，重新运行此脚本"

echo ""
echo "🚨 如果仍然报错，请检查："
echo "   1. Apple Developer 账号配置"
echo "   2. 证书和描述文件状态"
echo "   3. Bundle Identifier 是否正确"
echo "   4. iOS 部署目标版本"

echo ""
echo "⏰ 完成时间: $(date)"
echo "======================================"

# 自动打开 Xcode 工作空间
echo ""
echo "🚀 自动打开 Xcode 工作空间..."

# 查找 .xcworkspace 文件（实际上是目录）
WORKSPACE_FILE=$(find ios -name "*.xcworkspace" -type d -not -path "*/project.xcworkspace" | head -1)

if [ -n "$WORKSPACE_FILE" ] && [ -d "$WORKSPACE_FILE" ]; then
    echo "✅ 正在打开 Xcode 工作空间: $(basename "$WORKSPACE_FILE")"
    open "$WORKSPACE_FILE"
else
    echo "❌ 未找到 Xcode 工作空间文件"
    echo "💡 请手动打开: ios/玩真的.xcworkspace"
fi

# 等待用户按键后关闭
echo ""
echo "按任意键关闭窗口..."
read -n1 -s
